﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization;

namespace TurboTemplate
{
    public class TemplateHelper
    {
        public static TransformTemplate Load(string file)
        {
            var ret = file.Load<TransformTemplate>();
            ret.FileName = file;
            return ret;
        }

        public static void Save(TransformTemplate t, string file)
        {
            t.Save(file);
            t.FileName = file;
        }

        public static string TemplateDirectory()
        {
            string appData = Path.Combine(Application.UserAppDataPath, "Templates");

            if (!Directory.Exists(appData))
                Directory.CreateDirectory(appData);

            return appData;
        }

        public static List<TransformTemplate> GetTemplates()
        {
            var ret = new List<TransformTemplate>();

            // Get our files
            var files = Directory.GetFiles(TemplateDirectory());
            foreach (var s in files)
            {
                ret.Add(TemplateHelper.Load(s));
            }
            ret.Sort((x,y) => x.Name.CompareTo(y.Name));
            return ret;
        }

        public static string GetFileNameForNewTemplate(TransformTemplate t)
        {
            return Path.Combine(TemplateDirectory(), Guid.NewGuid().ToString()) + ".rzt";
        }

        public static void DeleteTemplate(TransformTemplate t)
        {
            if (t != null && !string.IsNullOrWhiteSpace(t.FileName))
                File.Delete(t.FileName);
        }

        public static void DoTransforms(params TransformInstructions[] input)
        {

            // Trick dynamic into working (it doesn't)
            bool loaded = typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly != null;

            foreach (var p in input)
            {
                try
                {

                    // Run the SQL?
                    if (p.ProcedureFromSchema != null && p.ProcedureFromSchema.ExecuteForData)
                        p.ModelForTransform.Data = SchemaHelper.ExecuteProcedure(p);

                    p.ModelForTransform.TransformComplete();

                    string template = p.Template.Template;
                    string result = RazorEngine.Razor.Parse(template, p.ModelForTransform);
                    p.TransformedText = result;

                }
                catch (RazorEngine.Templating.TemplateCompilationException parsingEx)
                {
                    p.TransformedText = string.Join("\r\n", parsingEx.Errors.Select(x => x.ToString()));
                    p.TransformedText += "\r\n" + parsingEx.Message;
                }
                catch (Exception ex)
                {
                    p.TransformedText = ex.Message;
                }
            }
        }
        
    }
}
